#include "mainwindow.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QServiceManager>
#include <QServiceInterfaceDescriptor>
#include <QCoreApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget* centralWidget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout(centralWidget);
    QPushButton* runButton = new QPushButton("Run BreakService",this);
    connect(runButton,SIGNAL(clicked()),this, SLOT(runService()));
    layout->addWidget(runButton);
    QPushButton* stopButton = new QPushButton("Stop BreakService",this);
    connect(runButton,SIGNAL(clicked()),this, SLOT(stopService()));
    layout->addWidget(stopButton);
    centralWidget->setLayout(layout);
    this->setCentralWidget(centralWidget);
    serviceName = "BreakService";
    serviceConfName = "breakservice.xml";
    serviceManager = new QServiceManager(this);
}

MainWindow::~MainWindow()
{

}

void MainWindow::runService()
{
    QString path = QCoreApplication::applicationDirPath() + "/" + serviceConfName;
    if(serviceManager->addService(path))
    {
        QList<QServiceInterfaceDescriptor> descriptors = serviceManager->findInterfaces(serviceName);
        QObject *implementationRef;
        implementationRef = serviceManager->loadInterface(descriptors[0], 0, 0);
    }
}

void MainWindow::stopService()
{
    serviceManager->removeService(serviceName);
}
